const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    /**开局玩家牌上方的定缺选项容器 */
    queYiMen: cc.Node = null;

    /**各方位玩家头像右上角的牌面标识容器 */
    dingques: cc.Node[] = [];

    /**定缺筒万条的选择包围框*/
    selected: cc.Node[] = [];

    /**定缺文字容器 */
    tips: cc.Label[] = [];

    start(){
        if(!cc.vv){
            return;
        }

        this.initView();
        this.initDingQue();
        this.initEventHandlers();
    }

   

    initView(){
        const gameChild = this.node.getChildByName('game');
        this.queYiMen = gameChild.getChildByName('dingque');
        this.queYiMen.active = cc.vv.gameNetMgr.isDingQueing;

        for(let key in cc.vv.directions){
            let side = gameChild.getChildByName(cc.vv.directions[key]);
            let seat = side.getChildByName('seat');
            let dingque = seat.getChildByName('que');
            this.dingques.push(dingque);
        }

        this.reset();

        const tips = this.queYiMen.getChildByName('tips');
        for(let i = 0; i < tips.childrenCount; i++){
            this.tips.push(tips.children[i].getComponent(cc.Label));
        }

        if(cc.vv.gameNetMgr.gamestate == 'dingque'){
            this.showDingQueChoice();
        }

        // 定缺选项添加点击事件
        ['tong', 'tiao', 'wan'].forEach((name) => {
            const node = this.queYiMen.getChildByName(name);
            cc.vv.utils.addClickEvent(node, this.node, 'DingQue', 'onQueYiMenClicked');
        });
    }
    
    /**
     * 初始化一些界面
     */
    reset(){
        this.setInteractable(true);

        this.selected.push(this.queYiMen.getChildByName('tong_selected'));
        this.selected.push(this.queYiMen.getChildByName('tiao_selected'));
        this.selected.push(this.queYiMen.getChildByName('wan_selected'));

        // 隐藏选择框
        for(let i = 0; i < this.selected.length; i++){
            this.selected[i].active = false;
        }

        // 将各方位玩家右上角的牌面标识隐藏掉
        for(let i = 0; i < this.dingques.length; i++){
            for(let j = 0; j < this.dingques[i].children.length; j++){
                this.dingques[i].children[j].active = false;
            }
        }
    }


    /**设置定缺的筒万条的是否可用状态 */
    setInteractable(value: boolean){
        this.queYiMen.getChildByName('tong').getComponent(cc.Button).interactable = value;
        this.queYiMen.getChildByName('tiao').getComponent(cc.Button).interactable = value;
        this.queYiMen.getChildByName('wan').getComponent(cc.Button).interactable = value;
    }



    /**
     * 显示系统推荐的牌面定缺选择跟动画
     */
    showDingQueChoice(){
        this.queYiMen.active = true;
        const sd = cc.vv.gameNetMgr.getSelfData();
        let typeCounts = [0,0,0];
        // 统计手牌筒万条各有多少张
        for(let i = 0; i < (sd.holds as number[]).length; i++){
            let pai = sd.holds[i];
            let type = cc.vv.mahjongmgr.getMahjongType(pai);
            typeCounts[type]++;
        }

        let min = 65535;
        let minIndex = 0;
        // 找到最少牌面的索引值
        for(let i = 0; i < typeCounts.length; i++){
            if(typeCounts[i] < min){
                min = typeCounts[i];
                minIndex = i;
            }
        }

        const arr = ['tong', 'tiao', 'wan'];
        // 初始化最少牌面字的动画显示
        for(let i = 0; i < arr.length; i++){
            const node =  this.queYiMen.getChildByName(arr[i]);
            if(minIndex == i){
                node.getComponent(cc.Animation).play('dingque_tuijian');
            }
            else{
                node.getComponent(cc.Animation).stop();
            }
        }

        this.reset();

        for(let i = 0; i < this.tips.length; i++){
            let n = this.tips[i];
            n.node.active = i > 0 ? false : true;
        }
    }


    /**
     * 初始化玩家右上角的牌面字
     */
    initDingQue(){
        const arr = ['tong', 'tiao', 'wan'];
        const data = cc.vv.gameNetMgr.seats;
        for(let i = 0; i < data.length; i++){
            let que = data[i].dingQue;
            if(que == null || que < 0 || que >= arr.length){
                que = null;
            }
            else{
                que = arr[que];
            }
            let localIndex = cc.vv.gameNetMgr.getLocalIndex(i);
            if(que){
                this.dingques[localIndex].getChildByName(que).active = true;
            }
        }
    }



    /**
     * 初始化监听定缺的各种事件
     */
    initEventHandlers(){

        this.node.on(cc.vv.game_event_map.GAME_DINGQUE, (data) => {
            this.showDingQueChoice();
        });

        this.node.on(cc.vv.game_event_map.GAME_DINGQUE_NOTIFY, (data) => {
            let seatIndex = cc.vv.gameNetMgr.getSeatIndexById(data.detail);
            let localIndex = cc.vv.gameNetMgr.getLocalIndex(seatIndex);
            this.tips[localIndex].node.active = true;
        });

        this.node.on(cc.vv.game_event_map.GAME_DINGQUE_FINISH, (data) => {
            this.queYiMen.active = false;
            cc.vv.gameNetMgr.isDingQueing = false;
            this.initDingQue();
        });
    }


    /**定缺选择的点击事件 */
    onQueYiMenClicked(event: cc.Event){
        let type = 0;
        switch(event.target.name){
            case 'tong':
                type = 0;
                break;
            case 'tiao':
                type = 1;
                break;
            case 'wan':
                type = 2;
                break;
        }

        this.selected.forEach((node) => {
            node.active = false;
        });
        this.selected[type].active = true;
        cc.vv.net.send(cc.vv.game_event_map.DINGQUE, type);
    }
}
